//compute kid marriage probabilities -- HS
use "$temp/acs_marriage_sample", clear
keep if year>=2008 & year<=2012 //limit year range
keep if age>=36 & age<=54 //limit age range
keep if relate == 1 | relate == 2 //household heads and spouses

****normalize by mean earnings
preserve
	use "$temp/wage_norm", clear
	su mean
	local norm `r(mean)'
restore

****normalize income
ren statefip statefips
merge m:1 statefips using "$temp/skill_prices_all", keep(match) nogen

replace skill_price_2010 = exp(log(skill_price_2010) - 0.145) if racetype == 2
replace skill_price_2010 = exp(log(skill_price_2010) - 0.087) if racetype == 3

replace skill_price_coll_2010 = exp(log(skill_price_coll_2010) - 0.145) if racetype == 2
replace skill_price_coll_2010 = exp(log(skill_price_coll_2010) - 0.087) if racetype == 3

replace incwage = incwage/skill_price_2010 if !coll //normalize by skill prices
replace incwage = incwage/skill_price_coll_2010 if coll //normalize by skill prices
replace incwage = incwage/`norm' //normalize by mean PSID earnings

replace incwage = incwage * (2080/hours) //hours normalization
ren incwage hc
drop if hc>6 & hc!=.
su hc [fw=perwt], d



//obtain spousal characteriststics: schooling, hc, work status
gen nilf = (hours==0)

//spousal characteristics
preserve
drop if relate==1
keep year serial  coll nilf hc 
ren coll coll_sp
ren nilf nilf_sp
ren hc hc_sp
tempfile spouse
save `spouse'
restore

//limit to working heads
keep if relate==1
merge 1:1 year serial using `spouse', keep(1 3) nogen
drop if flag_drop
gen married = (marst==1)
keep if married


//define types and collapse
gen type = 1 //nilf_sp == 1
replace type = 2 if !coll_sp & !nilf_sp
replace type = 3 if coll_sp & !nilf_sp
gen count = 1

//by state
preserve
collapse (sum) count (mean) spouse_hc_mean = hc_sp (sd) spouse_hc_sd = hc_sp [fw=perwt], by(type statefips coll)
sort statefips coll type


bys statefips coll: egen totalcount = total(count)
gen frac = count/totalcount
drop count totalcount
sort statefips coll type

//generate log-normal paramters
gen sp_hc_mean_log = log(spouse_hc_mean^2 / sqrt(spouse_hc_mean^2 + spouse_hc_sd^2))
gen sp_hc_sd_log = log(1 + (spouse_hc_sd^2)/(spouse_hc_mean^2))

keep statefips coll type frac sp_hc_mean_log sp_hc_sd_log
order statefips coll type frac sp_hc_mean_log sp_hc_sd_log
export delimited "$model/utilities/state_child_marr_distributions.csv", novarn replace
restore


//for later decomposition exercise
levelsof statefips, clean local(fips)
collapse (sum) count (mean) spouse_hc_mean = hc_sp (sd) spouse_hc_sd = hc_sp [fw=perwt], by(type coll)
sort coll type

bys  coll: egen totalcount = total(count)
gen frac = count/totalcount
drop count totalcount
sort coll type

//generate log-normal paramters
gen sp_hc_mean_log = log(spouse_hc_mean^2 / sqrt(spouse_hc_mean^2 + spouse_hc_sd^2))
gen sp_hc_sd_log = log(1 + (spouse_hc_sd^2)/(spouse_hc_mean^2))


foreach fip in `fips'{
    preserve
	gen statefips = `fip'
	save "$temp/marr_type_decomp_`fip'", replace
	restore
}

clear
foreach fip in `fips'{
	append using "$temp/marr_type_decomp_`fip'"
}


keep statefips coll type frac sp_hc_mean_log sp_hc_sd_log
order statefips coll type frac sp_hc_mean_log sp_hc_sd_log
export delimited "$model/utilities/state_child_marr_distributions_decomp_d.csv", novarn replace
